# _*_ coding: utf-8 _*_
__author__ = 'yinlei'
__date__ = '2018-05-19 10:14'
from functools import wraps
from flask import session,redirect,url_for,g
from config import CMS_USER_ID


def login_required(func):

    @wraps(func)
    def check_login(*args,**kwargs):
        if CMS_USER_ID in session:
            return func(*args,**kwargs)
        else:
            return  redirect(url_for('cms.login'))
    return check_login

def  permission_required(permission):
    def outer(func):
        @wraps(func)
        def inner(*args,**kwags):
            try:
                user =g.cms_user
            except:
                return redirect(url_for('cms.index'))
            if user and user.has_permission(permission):
                return func(*args,**kwags)
            else:
                return redirect(url_for('cms.index'))
        return inner
    return outer